//
//  Carouse.m
//  05-HomeWork
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "Carouse.h"
#import "UIImageView+DownloadImage.h"
@interface Carouse ()
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, assign) NSInteger index;
@end
#define SCREEN [UIScreen mainScreen].bounds
#define WIDTH self.bounds.size.width
#define HEIGHT self.bounds.size.height
@implementation Carouse
#pragma mark - 自定义初始化
- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array {
    self = [super initWithFrame:frame];
    if (self) {
        [self createArray:array];
    }
    return self;
}
#pragma mark - 调用Array参数
- (void)createArray:(NSArray *)array {
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.contentSize = CGSizeMake(WIDTH * (array.count + 2), 0);
    _scrollView.contentOffset = CGPointMake(WIDTH, 0);
    [self addSubview:_scrollView];
    _scrollView.backgroundColor = [UIColor cyanColor];
    // for 循环加入图片
    for (int i = 0; i < array.count; i++) {
        // 创建个小的scrollView
        UIScrollView *smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake((i + 1) * WIDTH, 0, WIDTH, HEIGHT)];
        self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [_image asyncDownloadImageWithURL:array[i] completionHandler:^(NSData *data, UIImage *image) {
            
        }];
//        _image.image = [UIImage imageNamed:array[i]];
        // 设置smallSCroll的属性
        smallScroll.delegate = self;
        smallScroll.minimumZoomScale = 0.5;
        smallScroll.maximumZoomScale = 2;
        [smallScroll addSubview:_image];
        [_scrollView addSubview:smallScroll];
        // 设置次数
        _index = i + 1;
    }
    // 创建第一个和最后一个scroll并插入到大scrollView的第一和最后的位置上
    UIScrollView *firstScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIScrollView *lastScroll = [[UIScrollView alloc] initWithFrame:CGRectMake((array.count + 1) * WIDTH, 0, WIDTH, HEIGHT)];
    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImage.image = [UIImage imageNamed:[array lastObject]];
    lastImage.image = [UIImage imageNamed:[array firstObject]];
    [firstScroll addSubview:firstImage];
    [lastScroll addSubview:lastImage];
    [_scrollView insertSubview:firstScroll atIndex:0];
    [_scrollView insertSubview:lastScroll atIndex:array.count + 1];
    // 设置两个scroll的属性
    firstScroll.delegate = self;
    lastScroll.delegate = self;
    firstScroll.minimumZoomScale = 0.5;
    firstScroll.maximumZoomScale = 2;
    lastScroll.minimumZoomScale = 0.5;
    lastScroll.maximumZoomScale = 2;
    // 设置大scrollview的属性
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    // 创建pageControl, 用来实现对应互动
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT * 5 / 6, WIDTH, 20)];
    _page.pageIndicatorTintColor = [UIColor redColor];
    [self addSubview:_page];
    _page.numberOfPages = _scrollView.contentSize.width / WIDTH - 2;
    [_page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark - 用来实现图片的缩放效果
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
#pragma mark - 轮播图 恢复大小
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 通过判断偏移量来实现轮播图
    if (scrollView.contentOffset.x == WIDTH * (_index + 1)) {
        scrollView.contentOffset = CGPointMake(WIDTH,0);
    } else if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(WIDTH * _index, 0);
    }
    // 实现恢复图片大小
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1.0;
        }
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 实现page的点跟随照片的滑动而变化
    _page.currentPage = scrollView.contentOffset.x / WIDTH - 1;

    }
#pragma mark - 实现点击page, 图片跳转
- (void)handlePage:(UIPageControl *)page {
    _scrollView.contentOffset = CGPointMake((page.currentPage + 1) * WIDTH, 0);
}



@end
